home *** CD-ROM | disk | FTP | other *** search
/ Joystick Magazine 1996 April / Joystick.Hors serie avril 96Flashback+demos.iso / wing / tblt.c_ / tblt.c
Text File  |  1994-12-08  |  4KB  |  141 lines

  1. /**************************************************************************
  2.  
  3.     TBLT.C - transparent blt
  4.  
  5.  **************************************************************************/
  6. /**************************************************************************
  7.  
  8.     (C) Copyright 1994 Microsoft Corp.  All rights reserved.
  9.  
  10.     You have a royalty-free right to use, modify, reproduce and 
  11.     distribute the Sample Files (and/or any modified version) in 
  12.     any way you find useful, provided that you agree that 
  13.     Microsoft has no warranty obligations or liability for any 
  14.     Sample Application Files which are modified. 
  15.  
  16.  **************************************************************************/
  17.  
  18. #include <windows.h>
  19. #include <windowsx.h>
  20. #include <wing.h>
  21.  
  22. #include "..\utils\utils.h"
  23.  
  24. extern  void PASCAL far TransCopyDIBBits( WORD DestSelector, DWORD DestOffset,
  25.   void const far *pSource, DWORD Width, DWORD Height, long DestWidth,
  26.   long SourceWidth, char unsigned TransparentColor );
  27.  
  28.  
  29. /*----------------------------------------------------------------------------
  30.  
  31. This function ignores the source origin and blts the entire source.
  32.  
  33. */
  34.  
  35. BOOL TransparentDIBits( BITMAPINFO far *pBufferHeader,
  36.   void huge *pBufferBits,
  37.   int nXOriginDest, int nYOriginDest, void const far *pBits,
  38.   BITMAPINFO const far *pBitmapInfo, int nXOriginSrc, int nYOriginSrc,
  39.   int iUsage, char unsigned TransparentColor )
  40. {
  41.   BOOL ReturnValue = FALSE;
  42.  
  43.   int NewDestinationXOrigin;
  44.   int NewDestinationYOrigin;
  45.   int DestinationDeltaX;
  46.   int DestinationDeltaY;
  47.   int Width;
  48.   int Height;
  49.   int NewSourceXOrigin;
  50.   int NewSourceYOrigin;
  51.   int Orientation = 1;
  52.   int DestinationHeight = DibHeight(&pBufferHeader->bmiHeader);
  53.   int SourceWidth = DibWidth(&pBitmapInfo->bmiHeader);
  54.   int SourceHeight = DibHeight(&pBitmapInfo->bmiHeader);
  55.  
  56.   char unsigned huge *pSource;
  57.   WORD DestSelector;
  58.   DWORD DestOffset;
  59.  
  60.   RECT SourceRectangle = { nXOriginDest, nYOriginDest,
  61.     nXOriginDest + SourceWidth, nYOriginDest + SourceHeight };
  62.  
  63.   RECT DestinationRectangle;
  64.  
  65.   RECT ClippedRectangle;
  66.  
  67.   if(DestinationHeight < 0)   // check for top-down DIB
  68.   {
  69.     Orientation = -1;
  70.     DestinationHeight = -DestinationHeight;
  71.   }
  72.  
  73.   DestinationRectangle.top = 0;
  74.   DestinationRectangle.left = 0;
  75.   DestinationRectangle.bottom = DestinationHeight;
  76.   DestinationRectangle.right = DibWidth(&pBufferHeader->bmiHeader);
  77.  
  78.   // intersect the destination rectangle with the destination DIB
  79.  
  80.   if(IntersectRect(&ClippedRectangle,&SourceRectangle,
  81.     &DestinationRectangle))
  82.   {
  83.     // default delta scan to width in bytes
  84.     long DestinationScanDelta = DibWidthBytes(&pBufferHeader->bmiHeader);
  85.  
  86.  
  87.     NewDestinationXOrigin = ClippedRectangle.left;
  88.     NewDestinationYOrigin = ClippedRectangle.top;
  89.  
  90.     DestinationDeltaX = NewDestinationXOrigin - nXOriginDest;
  91.     DestinationDeltaY = NewDestinationYOrigin - nYOriginDest;
  92.  
  93.     Width = ClippedRectangle.right - ClippedRectangle.left;
  94.     Height = ClippedRectangle.bottom - ClippedRectangle.top;
  95.  
  96.     pSource = (char unsigned huge *)pBits;
  97.  
  98.     NewSourceXOrigin = DestinationDeltaX;
  99.     NewSourceYOrigin = DestinationDeltaY;
  100.  
  101.     pSource += ((long)SourceHeight - (NewSourceYOrigin + Height)) *
  102.       (long)DibWidthBytes(&pBitmapInfo->bmiHeader)
  103.       + NewSourceXOrigin;
  104.  
  105.     // now we'll calculate the starting destination pointer taking into
  106.     // account we may have a top-down destination
  107.  
  108.     DestSelector = HIWORD(pBufferBits);
  109.  
  110.     if(Orientation < 0)
  111.     {
  112.       // destination is top-down
  113.  
  114.       DestinationScanDelta *= -1;
  115.  
  116.       DestOffset = (long)(NewDestinationYOrigin + Height - 1) *
  117.         (long)DibWidthBytes(&pBufferHeader->bmiHeader) +
  118.         NewDestinationXOrigin;
  119.     }
  120.     else
  121.     {
  122.       // destination is bottom-up
  123.  
  124.       DestOffset = ((long)DestinationHeight -
  125.         (NewDestinationYOrigin + Height))
  126.         * (long)DibWidthBytes(&pBufferHeader->bmiHeader) +
  127.         NewDestinationXOrigin;
  128.     }
  129.  
  130.     TransCopyDIBBits(DestSelector,DestOffset,pSource,Width,Height,
  131.       DestinationScanDelta,
  132.       DibWidthBytes(&pBitmapInfo->bmiHeader),
  133.       TransparentColor);
  134.  
  135.     ReturnValue = TRUE;
  136.   }
  137.  
  138.   return ReturnValue;
  139. }
  140.  
  141.